'use strict';

const Controller = require('egg').Controller;

class CreateapiController extends Controller {
  async createapi() {
    const { ctx, app } = this
    // 数据解析
    const { projectId: project_id, url: request_url, requestMethod: request_methods, requestParams, apiDesc: request_desc, reponseJson: response_json, remark, apiAuthor: api_author } = ctx.request.body
    requestParams.forEach((item, index, newarr) => {
      newarr[index] = JSON.stringify(item)
    })
    const request_params = requestParams.join('|')
    // 调用service
    const result = await ctx.service.api.createApi(project_id, request_url, request_methods, request_params, request_desc, response_json, remark, api_author)
    if (result.affectedRows === 1) {
      ctx.body = {
        code: 1,
        msg: '添加成功'
      }
      return
    }
    ctx.body = {
      code: 0,
      msg: '添加失败'
    }
  }
}

module.exports = CreateapiController;
